package com.example.actionsignin.mvp.view;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.actionsignin.R;
import com.example.actionsignin.fragment.ActionFragment;
import com.example.actionsignin.fragment.ClassFragment;
import com.example.actionsignin.fragment.ManageFragment;
import com.example.actionsignin.fragment.MineFragment;
import com.example.actionsignin.utils.ToastUtil;

/**
 * 主界面
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mImage_action;
    private ImageView mImage_manage;
    private ImageView mImage_mine;

    private LinearLayout linearLayout_action;
    private LinearLayout linearLayout_manage;
    private LinearLayout linearLayout_mine;

    private FragmentManager mFragmentManager;

    private ActionFragment actionFragment;
    private ManageFragment manageFragment;
    private MineFragment mineFragment;

    private boolean exit_flag = true;

    private static View view;

    private FloatingActionButton mFloatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragmentManager = getSupportFragmentManager();

        init();
        changeImage(0);

    }

    private void init() {
        linearLayout_action = (LinearLayout) findViewById(R.id.linear_action);
        linearLayout_action.setOnClickListener(this);

        linearLayout_manage = (LinearLayout) findViewById(R.id.linear_manage);
        linearLayout_manage.setOnClickListener(this);

        linearLayout_mine = (LinearLayout) findViewById(R.id.linear_mine);
        linearLayout_mine.setOnClickListener(this);

        mFloatingActionButton = (FloatingActionButton) findViewById(R.id.main_floatingActionBtn);
        mFloatingActionButton.setOnClickListener(this);

        mImage_action = (ImageView) findViewById(R.id.image_action);
        mImage_manage = (ImageView) findViewById(R.id.image_manage);
        mImage_mine = (ImageView) findViewById(R.id.image_mine);

        view = linearLayout_action;
    }

    private void hideFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();

        if (actionFragment != null) {
            transaction.hide(actionFragment);
        }
        if (manageFragment != null) {
            transaction.hide(manageFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }

        transaction.commit();
    }

    private void changeImage(int id) {
        hideImage();
        switch (id) {
            case 0:
                mImage_action.setImageResource(R.mipmap.action_2);
                break;
            case 2:
                mImage_manage.setImageResource(R.mipmap.manage_2);
                break;
            case 3:
                mImage_mine.setImageResource(R.mipmap.mine_2);
                break;
        }
    }

    private void hideImage() {
        mImage_action.setImageResource(R.mipmap.action_1);
        mImage_manage.setImageResource(R.mipmap.manage_1);
        mImage_mine.setImageResource(R.mipmap.mine_1);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        FragmentTransaction transaction = mFragmentManager.beginTransaction();

        switch (id) {
            case R.id.linear_action:
                view = linearLayout_action;
                hideFragment();
                if (actionFragment == null) {
                    actionFragment = new ActionFragment();
                    transaction.add(R.id.fragment_main, actionFragment);
                } else {
                    transaction.show(actionFragment);
                }

                changeImage(0);
                break;
            case R.id.linear_manage:
                view = linearLayout_manage;
                hideFragment();
                if (manageFragment == null) {
                    manageFragment = new ManageFragment();
                    transaction.add(R.id.fragment_main, manageFragment);
                } else {
                    transaction.show(manageFragment);
                }

                changeImage(2);
                break;
            case R.id.linear_mine:
                view = linearLayout_mine;
                hideFragment();
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.fragment_main, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }

                changeImage(3);
                break;
            case R.id.main_floatingActionBtn:
                Intent intent = new Intent(MainActivity.this, ReleaseActivity.class);
                MainActivity.this.startActivity(intent);
                break;
        }

        transaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();

        onClick(view);
    }


    @Override
    public void onBackPressed() {

        if (exit_flag) {
            exit_flag = !exit_flag;
            ToastUtil.INSTANCE(this, "再按一次退出程序");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3 * 1000);
                        exit_flag = !exit_flag;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } else {
            moveTaskToBack(true);
        }

    }
}
